/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.ad.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.common.shiro.realm.LoginInfo;
import com.jeesite.modules.ad.dao.AdAgentDao;
import com.jeesite.modules.ad.dao.AdVertiserDao;
import com.jeesite.modules.ad.entity.Ad;
import com.jeesite.modules.ad.entity.AdAgent;
import com.jeesite.modules.ad.entity.AdGuest;
import com.jeesite.modules.ad.entity.AdVertiser;
import com.jeesite.modules.file.utils.FileUploadUtils;
import com.jeesite.modules.sys.dao.UserRoleDao;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.entity.UserRole;
import com.jeesite.modules.sys.service.UserService;
import com.jeesite.modules.sys.utils.PwdUtils;
import com.jeesite.modules.sys.utils.UserUtils;

/**
 * 广告主Service
 * 
 * @author chenxm
 * @version 2019-04-16
 */
@Service
@Transactional(readOnly = true)
public class AdVertiserService extends CrudService<AdVertiserDao, AdVertiser> {

	@Autowired
	private UserService userService;

	@Autowired
	private UserRoleDao userRoleDao;

	@Autowired
	private AdAgentService adAgentService;

	@Autowired
	private AdService adService;

	@Autowired
	private AdGuestService adGuestService;

	@Autowired
	private AdAgentDao adAgentDao;

	public AdVertiser getCurVertiser() {
		LoginInfo loginInfo = UserUtils.getLoginInfo();
		AdVertiser curAgent = this.getByUserCode(loginInfo.getId());
		return curAgent;
	}

	public AdVertiser getByUserCode(String userCode) {
		AdVertiser a = new AdVertiser();
		a.setUserCode(userCode);
		List<AdVertiser> list = this.dao.findList(a);
		return ListUtils.isEmpty(list) ? null : list.get(0);
	}

	/**
	 * 获取单条数据
	 * 
	 * @param adVertiser
	 * @return
	 */
	@Override
	public AdVertiser get(AdVertiser adVertiser) {
		return super.get(adVertiser);
	}

	public AdVertiser getByAccount(AdVertiser a) {
		return this.dao.getByAccount(a);
	}

	/**
	 * 查询分页数据
	 * 
	 * @param adVertiser 查询条件
	 * @return
	 */
	@Override
	public Page<AdVertiser> findPage(AdVertiser adVertiser) {
		User user = UserUtils.getUser();
		if (user.getMgrType().equals("0")) {
			// 不是管理员的情况,查询此登录用户对应的代理商的ID对应的广告主列表
			AdAgent adAgent = adAgentService.getCurAgent();
			if (adAgent != null) {
				adVertiser.setAgentId(adAgent.getId());
			}
		}
		return super.findPage(adVertiser);
	}

	/**
	 * 保存数据（插入或更新）
	 * 
	 * @param adVertiser
	 */
	@Override
	@Transactional(readOnly = false)
	public void save(AdVertiser adVertiser) {
		adVertiser.setAgentId(adAgentService.getCurAgent().getId());
		if (adVertiser.getIsNewRecord()) {
			LoginInfo loginInfo = UserUtils.getLoginInfo();
			AdAgent curAgent = this.adAgentService.getByUserCode(loginInfo.getId());
			if (curAgent != null) {
				// 代理商增加扩展广告主数
				this.adAgentService.addCustomerNum(curAgent);
			}

			User u = new User();
			BeanUtils.copyProperties(adVertiser, u);
			u.setLoginCode(adVertiser.getAccount());
			u.setUserName(adVertiser.getAccount());
			userService.save(u);

			adVertiser.setUserCode(u.getUserCode());
			super.save(adVertiser);

			UserRole ur = new UserRole();
			ur.setRoleCode("ggz");
			ur.setUserCode(u.getUserCode());
			this.userRoleDao.insert(ur);
		} else {
			// 修改广告主信息
			User u = this.userService.get(adVertiser.getAccount());
			BeanUtils.copyProperties(adVertiser, u);
			u.setPassword(PwdUtils.encryptPassword(adVertiser.getPassword()));
			userService.save(u);
			super.save(adVertiser);
		}
		// 保存上传附件
		FileUploadUtils.saveFileUpload(adVertiser.getId(), "adVertiser_YYZZ");
		FileUploadUtils.saveFileUpload(adVertiser.getId(), "adVertiser_ICP");
		FileUploadUtils.saveFileUpload(adVertiser.getId(), "adVertiser_otherFile");

	}

	/**
	 * 更新状态
	 * 
	 * @param adVertiser
	 */
	@Override
	@Transactional(readOnly = false)
	public void updateStatus(AdVertiser adVertiser) {
		super.updateStatus(adVertiser);
	}

	/**
	 * 删除数据
	 * 
	 * @param adVertiser
	 */
	@Override
	@Transactional(readOnly = false)
	public void delete(AdVertiser adVertiser) {
		if (adVertiser != null) {
			String id = adVertiser.getId();
			if (!StringUtils.isBlank(id)) {
				// 广告
				Ad where = new Ad();
				where.setVertiserId(id);
				if (adService.findCount(where) > 0) {
					throw newValidationException("广告主含有广告计划,无法删除");
				}
				// 获客
				AdGuest where2 = new AdGuest();
				where2.setAdVertiserId(id);
				if (adGuestService.findCount(where2) > 0) {
					throw newValidationException("广告主含有获客信息,无法删除");
				}
			}
		}
		// 获取广告主对应用户编码
		String userCode = adVertiser.getUserCode();
		// 删除用户
		adAgentDao.deleteUser(userCode);
		// 删除用户对应角色
		adAgentDao.deleteUserAndAuth(userCode);
		super.delete(adVertiser);
	}

}